package org.example.mybatisplus.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.protobuf.ServiceException;
import org.example.mybatisplus.dao.entity.Student;
import org.example.mybatisplus.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 学生管理控制器 restful风格
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    /**
     * 保存用户信息
     * <p>
     * 根据用户ID查询学生对象详细信息。
     *
     * @param student 学生对象
     * @return 学生对象
     */
    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    @DS("slave")
    public String saveOrUpdate(@RequestBody Student student) {
        student.setCreateTime(new Date());
        student.setCreatePerson("system");
        studentService.saveOrUpdate(student);
        return "success";
    }

    /**
     * 查询学生列表信息
     * <p>

     * @return List<Student> 学生信息
     */
    @GetMapping("/list")
    public List<Student> list(String name) throws ServiceException {
        List<Student> list = studentService.lambdaQuery().like(Student::getName, name).list();
        /*if(0==0){
            throw new ServiceException("xxxxxx");
        }*/
        return list;
    }

    /**
     * 删除学生信息
     * @param pageNo 页码
     * @param pageSize 页大小
     * @return Page<Student>
     */
    @GetMapping("/page")
    public Page<Student> getAllHospital(Integer pageNo, Integer pageSize) {
        Page<Student> page = studentService.lambdaQuery()
                .ge(Student::getAge,22)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public String delete(Integer id) {
        studentService.removeById(id);
        return "success";
    }

}